package com.dmooo.ymx.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dmooo.ymx.R;
import com.dmooo.ymx.base.BaseActivity;
import com.dmooo.ymx.bean.Response;
import com.dmooo.ymx.bean.ShopActicleBean;
import com.dmooo.ymx.bean.VersionInformationBean;
import com.dmooo.ymx.common.CommonUtils;
import com.dmooo.ymx.common.T;
import com.dmooo.ymx.config.Constants;
import com.dmooo.ymx.https.HttpUtils;
import com.dmooo.ymx.https.onOKJsonHttpResponseHandler;
import com.dmooo.ymx.utils.APKVersionCodeUtils;
import com.dmooo.ymx.utils.DateUtils;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cz.msebera.android.httpclient.Header;
import me.drakeet.materialdialog.MaterialDialog;

public class AboutActivity extends BaseActivity {
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.version)
    TextView version;
    @BindView(R.id.tv_nine)
    TextView tvNine;
    @BindView(R.id.web_detail)
    WebView webDetail;

    @Override
    protected void initUI() {
        setContentView(R.layout.activity_about);
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        tvTitle.setText("关于我们");
        tvLeft.setVisibility(View.VISIBLE);
        version.setText("当前版本:"+APKVersionCodeUtils.getVerName(this));
        RequestParams shopParams = new RequestParams();
        shopParams.put("article_id", 27);
        HttpUtils.post(Constants.MESSAGE_ARTICLE_GETARTICLEMSG_URL, shopParams, new onOKJsonHttpResponseHandler<ShopActicleBean>(new TypeToken<Response<ShopActicleBean>>() {
        }) {

            @Override
            public void onStart() {
                super.onStart();
                showLoadingDialog();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                showToast(responseString);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeLoadingDialog();
            }

            @Override
            public void onSuccess(int statusCode, Response<ShopActicleBean> datas) {
                if (datas.isSuccess()) {
                    ShopActicleBean.ArticleAsg article_msg = datas.getData().getArticle_msg();
                    if (article_msg != null) {
                        webDetail.loadDataWithBaseURL(Constants.APP_IP,article_msg.getContent()==null?"":article_msg.getContent().replaceAll("<img","<img style='width:100%'"),"text/html","UTF-8","");
                    }
                } else {
                    showToast(datas.getMsg());
                }
            }
        });
    }

    @Override
    protected void initListener() {

    }

    @OnClick({R.id.tv_left, R.id.tv_nine, R.id.txt_q, R.id.txt_w})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_nine:
                ArticleRequest();
                break;
            case R.id.txt_w:
                NewsActivity.actionStart(this,"48", "隐私政策");
                break;
            case R.id.txt_q:
                NewsActivity.actionStart(this,"1", "用户协议");
                break;
        }
    }
    /**
     * @属性:获取版本信息
     * @开发者:陈飞
     * @时间:2018/7/21 17:14
     */
    private void ArticleRequest() {
        RequestParams requestParams = new RequestParams();
        HttpUtils.post(Constants.MESSAGE_ARTICLE_VERSION_URL, requestParams, new onOKJsonHttpResponseHandler<VersionInformationBean>(new TypeToken<Response<VersionInformationBean>>() {
        }) {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                showToast(responseString);
            }

            @Override
            public void onStart() {
                super.onStart();
                showLoadingDialog();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                closeLoadingDialog();
            }

            @Override
            public void onSuccess(int statusCode, Response<VersionInformationBean> datas) {
                if (datas.isSuccess()) {
                    final VersionInformationBean data = datas.getData();

                    if (data != null) {
                        //对比当前版本
                        if (CommonUtils.compareVersion(APKVersionCodeUtils.getVerName(getComeActivity()), data.getVersion()) == -1) {
                            showTipDialog("发现新版本", data.getContent(), new onClickListener() {
                                @Override
                                public void onClickSure() {
                                    builder = UpdateService.Builder.create(data.getDown_android()).build(getComeActivity());
                                    downDilog.show();
                                    isDownLoadFinish = false;
                                    new Thread(new MyThread()).start();
                                }
                            }, null);
                        } else {
                            showTipDialog("版本信息", Html.fromHtml("当前版本已经是最新版啦<br>版本号：" + data.getVersion() + "</b>"));

                        }
                    }

                } else {
                    showToast(datas.getMsg());
                }
            }
        });
    }
    private MaterialDialog downDilog;
    private View downLayout;
    private ProgressBar downProgressBar;
    private UpdateService.Builder builder;
    private boolean isDownLoadFinish = false;
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            // 要做的事情
            downProgressBar.setProgress(builder.getUpdateProgress());
            if (builder.getUpdateProgress() >= 99) {
                isDownLoadFinish = true;
                UpdateService.Builder.updateProgress = 1;
                downDilog.dismiss();
            }
            if (builder.getUpdateProgress() == -1) {
                UpdateService.Builder.updateProgress = 1;
                downDilog.dismiss();
                isDownLoadFinish = true;
                T.showShort(getComeActivity(), "下载更新失败");
            }
            super.handleMessage(msg);
        }
    };

}
